package com.example.second_home_work

import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private val expert = ProgramExpert()
    private lateinit var linearLayout1: LinearLayout
    private var counter = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        linearLayout1 = findViewById(R.id.linearLayout1)
        val button: Button = findViewById(R.id.button)
        val spinner: Spinner = findViewById(R.id.spinner)
        val textView: TextView = findViewById(R.id.textView)

        // 设置Spinner的适配器
        ArrayAdapter.createFromResource(
            this,
            R.array.list,
            android.R.layout.simple_spinner_item
        ).also { adapter ->
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            spinner.adapter = adapter
        }

        // 动态添加TextView到区1
        val addButton: Button = findViewById(R.id.button2)
        addButton.setOnClickListener {
            val newTextView = TextView(this)
            newTextView.text = getString(R.string.new_text)+" "+counter
            linearLayout1.addView(newTextView)
            counter++
        }

        // 区2的按钮点击事件
        button.setOnClickListener {
            val selected = spinner.selectedItem as String
            textView.text = expert.getLanguage(selected)
        }
    }
}